/* Class SinhVien */

function SinhVien(ms, ht, dc, dt, gt, ns, e) {
    this.ma_so = ms;
    this.ho_ten = ht;
    this.dia_chi = dc;
    this.dien_thoai = dt;
    this.gioi_tinh = gt;
    this.ngay_sinh = ns;
    this.email = e;
}

function DanhSach() {
    this.ds = new Array();
    this.sort = 0; /* 0  : BT
		      1  : MS tang dan
		      -1 : MS Giam dan
		      2  : HT tang dan
		      -2 : HT giam dan
		      3  : GT tang dan
		      -3 : GT giam dan
		      4  : NS tang dan
		      -4 : NS giam dan */
}

var DanhSachHocSinh = new DanhSach();

DanhSach.prototype.length = function () {
    return this.ds.length;
}

DanhSach.prototype.them_hs = function (hs) {
    this.ds.push(hs);
}
/* sap xep tang dan */
DanhSach.prototype.sx_ht_tang_dan = function() {
    this.ds = this.ds.sort(function(a, b) { return a.ho_ten.localeCompare(b.ho_ten) });
}

DanhSach.prototype.sx_ms_tang_dan = function() {
    this.ds = this.ds.sort(function(a, b) { return a.ma_so - b.ma_so });
}

DanhSach.prototype.sx_gt_tang_dan = function() {
    this.ds = this.ds.sort(function(a, b) { return a.gioi_tinh.localeCompare(b.gioi_tinh) });
}

DanhSach.prototype.sx_ngs_tang_dan = function() {
    this.ds = this.ds.sort(function(a, b) { return a.ngay_sinh.getTime() - b.ngay_sinh.getTime() });
}
/* Sap xep giam dan */
DanhSach.prototype.sx_ht_giam_dan = function() {
    this.ds = this.ds.sort(function(a, b) { return b.ho_ten.localeCompare(a.ho_ten) });
}

DanhSach.prototype.sx_ms_giam_dan = function() {
    this.ds = this.ds.sort(function(a, b) { return b.ma_so - a.ma-so });
}

DanhSach.prototype.sx_gt_giam_dan = function() {
    this.ds = this.ds.sort(function(a, b) { return b.gioi_tinh.localeCompare(a.gioi_tinh) });
}

DanhSach.prototype.sx_ngs_giam_dan = function() {
    this.ds = this.ds.sort(function(a, b) { return b.ngay_sinh.getTime() - a.ngay_sinh.getTime() });
}

/* Class MonHoc */
function MonHoc() {
    this.ten = t;
}

/* cau1 */
function tooltip(div, noi_dung) {
    div.style.display = 'inline';
    div.style.position = 'absolute';
    div.style.width = '170';
    div.style.backgroundColor = 'yellow';
    div.style.border = 'solid 1px black';
    div.innerHTML = noi_dung;
}

function hide_tooltip(div)
{
    div.style.display = 'none';
}


function boi_vang(div) {
    div.style.backgroundColor = 'yellow';
}

function bo_boi_vang(div) {
    div.style.backgroundColor = 'white';
}
/*** viet ham xoa_dk ***/
/* cau 2 */
function check_dk() {
    var alert_text = "";
    if (
	document.getElementById("ma_so").value.length == 0 ||
	document.getElementById("ho_ten").value == 0 ||
	document.getElementById("dia_chi").value == 0
	)
	{
	    alert_text += "Bạn chưa điền đầy đủ thông tin\n";
	}

    /* ktra b */
    var regexp_a = /\d{3}-\d{6}/i;
    var regexp_b = /\d{6}/i;

    var so_dien_thoai = document.getElementById("dien_thoai").value;
    if (regexp_a.test(so_dien_thoai) == false && regexp_b.test(so_dien_thoai) == false)
	{
	    alert_text += "Số điện thoại không hợp lệ\n";
	}
    if (document.getElementById("cb_nam").checked == false &&
	document.getElementById("cb_nu").checked == false)
	{
	    alert_text += "Chưa chọn giới tính!\n";
	}
    /* ktra ngay sinh */
    var ngay = parseInt(document.getElementById("ngs_ngay").value);
    var nam = parseInt(document.getElementById("ngs_nam").value);
    /* ktra email */
    var email_value = document.getElementById("email").value;
    var email_split = email_value.split("@");
    if (email_split[1] !== 'yahoo.com' && email_split[1] !== 'gmail.com')
	{
	    alert_text += "Email không hợp lệ!\n"
	}
    if (alert_text.length != 0)
	{
	    alert(alert_text);
	    return;
	}

    them_hs();
}

/* cau 3 */
function them_xoa_mh (btn_name) {
    var cmh_sb = document.getElementById("cmh");
    var cmhc_sb = document.getElementById("cmhc");

    // if (cmh_sb.selectedIndex < 0 || cmhc_sb.selectedIndex < 0)
    // 	return;

    switch (btn_name) {
    	case ">":
    	var option = document.createElement("option");
    	option.text = cmh_sb.options[cmh_sb.selectedIndex].innerHTML;
    	cmhc_sb.add(option, null);
    	cmh_sb.remove(cmh_sb.selectedIndex);
    	break;

    	case ">>":
	while (cmh_sb.length != 0)
    	    {
    		var option = document.createElement("option");
    		option.text = cmh_sb.options[0].innerHTML;
    		cmhc_sb.add(option, null);
    		cmh_sb.remove(0);
    	    }
    	break;

    	case "<":
    	var option = document.createElement("option");
    	option.text = cmhc_sb.options[cmhc_sb.selectedIndex].innerHTML;
    	cmh_sb.add(option, null);
    	cmhc_sb.remove(cmhc_sb.selectedIndex);
    	break;

    	case "<<":
	while (cmhc_sb.length != 0)
    	    {
    		var option = document.createElement("option");
    		option.text = cmhc_sb.options[0].innerHTML;
    		cmh_sb.add(option, null);
    		cmhc_sb.remove(0);
    	    }
    	break;
    }
}

function ok_btn() {
    var cmhc_sb = document.getElementById("cmhc");
    var alert_text = "";
    for (var i = 0; i < cmhc_sb.length; i++)
	{
	    alert_text += cmhc_sb.options[i].innerHTML;
	    alert_text += "\n";
	}
    alert(alert_text);
}

/* Ham them hoc sinh record */
function them_hs() {
    /* tao object hoc sinh */
    var ngs_thang = document.getElementById('ngs_thang'); // ???
    var ngs_nam = document.getElementById('ngs_nam').value;
    var i_ngs_ngay = document.getElementById('ngs_ngay').value;
    var ngs_thang_val  = ngs_thang.options[ngs_thang.selectedIndex].value; // ???
    var hs_ms = document.getElementById('ma_so').value;
    var hs_ten = document.getElementById('ho_ten').value;
    var hs_dc = document.getElementById('dia_chi').value;
    var hs_dt = document.getElementById('dien_thoai').value;
    var hs_gt = document.getElementById('cb_nam').checked == true ? "Nam" : "Nữ";
    var hs_ns = new Date (ngs_nam,
			  ngs_thang_val,
			  i_ngs_ngay);
			  
    var hs_e = document.getElementById('email').value;
    var hs = new SinhVien(hs_ms, hs_ten, hs_dc, hs_dt, hs_gt, hs_ns, hs_e);
    
    DanhSachHocSinh.them_hs(hs);
    ve_bang();

}

/* Cau 5 */
function sap_xep(btnname) {
    switch (btnname)
    {
	case "thmaso":
	if (DanhSachHocSinh.sort == 1) //
	    {
		DanhSachHocSinh.sx_ms_giam_dan();
		DanhSachHocSinh.sort = -1;
		break;
	    }
	DanhSachHocSinh.sx_ms_tang_dan();
	DanhSachHocSinh.sort = 1;
	break;

	case "thhoten":
	if (DanhSachHocSinh.sort == 2)
	    {
		DanhSachHocSinh.sx_ht_giam_dan();
		DanhSachHocSinh.sort = -2;
		break;
	    }
	DanhSachHocSinh.sx_ht_tang_dan();
	DanhSachHocSinh.sort = 2;
	break;

	case "thgioitinh":
	if (DanhSachHocSinh.sort == 3)
	    {
		DanhSachHocSinh.sx_gt_giam_dan();
		DanhSachHocSinh.sort = -3;
		break;
	    }
	DanhSachHocSinh.sx_gt_tang_dan();
	DanhSachHocSinh.sort = 3;
	break;

	case "thngaysinh":
	if (DanhSachHocSinh.sort == 4)
	    {
		DanhSachHocSinh.sx_ngs_giam_dan();
		DanhSachHocSinh.sort = -4;
		break;
	    }
	DanhSachHocSinh.sx_ngs_tang_dan();
	DanhSachHocSinh.sort = 4;
	break;
    }
    ve_bang();
}


function ve_bang() {
    var tab = document.getElementById('dsddk');
    if (tab.rows.length > 1)
	while (tab.rows.length != 1)
	    tab.deleteRow(1);
	
    for (var i = 0; i < DanhSachHocSinh.length(); i++)
	{
	    var row = document.createElement('tr');
	    /* boi mau cho dong neu re chuot */
	    row.setAttribute("onMouseOver", "boi_vang(this)");
	    row.setAttribute("onMouseOut", "bo_boi_vang(this)");

	    /* ma so, ho ten, gioi tinh, ngay sinh */
	    var cell1 = document.createElement('td');
	    cell1.appendChild(document.createTextNode(DanhSachHocSinh.ds[i].ma_so));
	    row.appendChild(cell1);

	    var cell2 = document.createElement('td');
	    cell2.appendChild(document.createTextNode(DanhSachHocSinh.ds[i].ho_ten));
	    row.appendChild(cell2);

	    var cell3 = document.createElement('td');
	    cell3.appendChild(document.createTextNode(DanhSachHocSinh.ds[i].gioi_tinh));
	    row.appendChild(cell3);

	    var cell4 = document.createElement('td');
	    cell4.appendChild(document.createTextNode(DanhSachHocSinh.ds[i].ngay_sinh));
	    row.appendChild(cell4);



	    tab.appendChild(row);
	}
}
